package com.ec.system.modules.sys.controller;

import com.ec.core.common.core.api.ResponseMsg;
import com.ec.core.common.core.api.Result;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.core.log.annotations.OperLog;
import com.ec.core.mybatis.groups.AddGroup;
import com.ec.core.mybatis.groups.SelectGroup;
import com.ec.core.mybatis.groups.UpdateGroup;
import com.ec.core.secure.annotations.PreAuth;
import com.ec.system.modules.sys.dto.PersonalInfoDTO;
import com.ec.system.modules.sys.dto.QueryUserDTO;
import com.ec.system.modules.sys.dto.ResetPasswordDTO;
import com.ec.system.modules.sys.dto.SysUserDTO;
import com.ec.system.modules.sys.service.ISysUserService;
import com.ec.system.modules.sys.vo.SysUserDetailVO;
import com.ec.system.modules.sys.vo.SysUserVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 用户接口
 *
 * @author: Eric Lee
 * @time: 2021/7/21 14:22
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private ISysUserService sysUserService;

    /**
     * 用户列表
     *
     * @param listUserDTO
     * @return
     */
    @GetMapping("/list")
    @PreAuth("sys_user_view")
    public Result<BasePageVO<SysUserVO>> listUser(QueryUserDTO listUserDTO) {
        return ResponseMsg.success(sysUserService.listUser(listUserDTO));
    }

    /**
     * 获取用户详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public Result<SysUserDetailVO> getUserDetail(@PathVariable Long userId) {
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setUserId(userId);
        return ResponseMsg.success(sysUserService.getUserDetail(userDTO));
    }

    /**
     * 查看用户信息
     *
     * @param userDTO
     * @return
     */
    @GetMapping("/checkUserInfo")
    @PreAuth("sys_user_view")
    public Result<SysUserDetailVO> checkUserInfo(@Validated(SelectGroup.class) SysUserDTO userDTO) {
        return ResponseMsg.success(sysUserService.checkUserInfo(userDTO));
    }

    @DeleteMapping("/{tenantId}/{userId}")
    @PreAuth("sys_user_delete")
    @OperLog(desc = "删除用户")
    public Result<Void> deleteUser(@PathVariable long tenantId, @PathVariable long userId) {
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setUserId(userId);
        userDTO.setTenantId(tenantId);
        return ResponseMsg.status(sysUserService.deleteUser(userDTO));
    }

    @PostMapping
    @PreAuth("sys_user_add")
    @OperLog(desc = "添加用户")
    public Result<Void> addUser(@Validated(AddGroup.class) @RequestBody SysUserDTO userDTO) {
        return ResponseMsg.status(sysUserService.addUser(userDTO));
    }

    @PutMapping
    @PreAuth("sys_user_edit")
    @OperLog(desc = "修改用户")
    public Result<Void> updateUser(@Validated(UpdateGroup.class) @RequestBody SysUserDTO userDTO) {
        return ResponseMsg.status(sysUserService.updateUser(userDTO));
    }

    @PutMapping("/changeUserStatus")
    @PreAuth("sys_user_edit")
    @OperLog(desc = "修改用户状态")
    public Result<Void> changeStatus(@RequestParam Long userId, @RequestParam Integer userStatus) {
        return ResponseMsg.status(sysUserService.changeUserStatus(userId, userStatus));
    }

    @PutMapping("/modifiedPersonalInfo")
    public Result<Void> modifiedPersonalInfo(@Validated(UpdateGroup.class) @RequestBody PersonalInfoDTO personalInfoDTO) {
        return ResponseMsg.status(sysUserService.modifiedPersonalInfo(personalInfoDTO));
    }

    /**
     * 重置个人密码
     *
     * @param resetPasswordDTO
     * @return
     */
    @PutMapping("/resetPassword")
    public Result<Void> resetPassword(@Validated(UpdateGroup.class) @RequestBody ResetPasswordDTO resetPasswordDTO) {
        return ResponseMsg.status(sysUserService.resetPassword(resetPasswordDTO));
    }

    /**
     * 修改用户密码
     *
     * @param resetPasswordDTO
     * @return
     */
    @PutMapping("/modifiedPassword")
    @PreAuth("sys_user_modified_pwd")
    @OperLog(desc = "修改用户密码")
    public Result<Void> modifiedPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        return ResponseMsg.status(sysUserService.modifiedPassword(resetPasswordDTO));
    }

    @GetMapping("/generateAccount/{tenantId}")
    public Result<String> generateAccount(@PathVariable Long tenantId) {
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setTenantId(tenantId);
        return ResponseMsg.success(sysUserService.generateAccount(userDTO));
    }


}
